package com.xk857.task;


import com.xk857.db.CarDB;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * 存放定时任务
 *
 * @Author CV大魔王
 * @Date 2020.1.20
 */
@Component
public class DefaultTask {


    /**
     * 模拟ETC扣费
     * 30s扣费20元
     */
    @Scheduled(cron = "*/30 * * * * ?")
    public void etcDeduction() {
        CarDB.carList.forEach(car -> car.setMoney(car.getMoney() - 20));
    }

    /**
     * 模拟小车速度变化，每30s执行一次
     */
    @Scheduled(cron = "*/30 * * * * ?")
    public void carSpeed() {
        CarDB.carList.forEach(car -> {
            Random random = new Random();
            car.setSpeed(random.nextInt(90) + 10);
            //如果小车没钱了，则停止运行，模拟ETC扣费
            if (car.getMoney() <= 0){
                car.setSpeed(0);
            }
        });
    }

}
